package com.hcmu.view.MainHome;

import vanhoc.android.utils.ResourceUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.telpoo.truyencuoi.R;
import com.hcmu.network.utils.ConnectionDetector;

/**
 * @author SEVEN
 *
 */
public class MainHome extends Activity {
    Boolean _isFirst = true;
    ImageButton btnStart;
    ResourceUtils res;
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_mainhome);
		
		res = new ResourceUtils(this);
		final ConnectionDetector connection = new ConnectionDetector(this);
		
		
		if (connection.isConnectingToInternet()) { // doc noi dung thong qua rss
			showDialog1();
		}else{
			showDialog2();
		}
		
		
		btnStart = (ImageButton) findViewById(R.id.btnStart);
		btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
    }

    private void showDialog1() {
		// TODO Auto-generated method stub
    	final AlertDialog dialog = new AlertDialog.Builder(this).create();
    	dialog.setTitle(res.getString(R.string.SELECT_READ_MODE));
    	
    	
    	dialog.setButton("Trực tuyến", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				startActivity(new Intent(getApplicationContext(), OnlineMainView.class));
				dialog.dismiss();
				finish();
			}
		});
    	
    	dialog.setButton2("Đọc trên máy", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				startActivity(new Intent(getApplicationContext(), OfflineMainView.class));
				dialog.dismiss();
				finish();
			}
		});
    	
    	dialog.show();
	}
    
    
    private void showDialog2() {
		// TODO Auto-generated method stub
    	final AlertDialog dialog = new AlertDialog.Builder(this).create();
    	dialog.setTitle(res.getString(R.string.INTERNET_CONNECTION_FAIL));
    	
    	
    	dialog.setButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				startActivity(new Intent(getApplicationContext(), OfflineMainView.class));
				dialog.dismiss();
			}
		});
    	
    	dialog.setButton2("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});
    	
    	dialog.show();
	}
    
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
}
